package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tang
 * @data 2021/9/1
 * @apiNode
 */
@Service
public class RightsServiceImpl implements RightsService{
    @Autowired
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> findAll() {
        return rightsMapper.selectList(null);
    }

    @Override
    public List<Rights> getRightsList() {
        /**
         * 实现思路:
         *      1.先查询一级菜单信息  parent_id = 0
         *      2.将一级菜单循环遍历  一级菜单对象
         *      3.根据一级菜单信息,查询当前菜单下的二级
         *      4.将还查询得到的二级菜单,封装到一级菜单对象中
         * 实现思路二:
         *      利用左连接  实现关联查询   封装数据
         */
        QueryWrapper<Rights> rightsQueryWrapper = new QueryWrapper<>();
        rightsQueryWrapper.eq("parent_id", 0);
        List<Rights> onesList = rightsMapper.selectList(rightsQueryWrapper);
        for (Rights onerights : onesList) {
            rightsQueryWrapper.clear();
                                                    //根据一级id查询二级
            rightsQueryWrapper.eq("parent_id", onerights.getId());
            List<Rights> twoList = rightsMapper.selectList(rightsQueryWrapper);
            onerights.setChildren(twoList);
        }
        return onesList;
    }
}
